package com.atguigu.school.controller;

import com.atguigu.school.commons.Page;
import com.atguigu.school.commons.Result;
import com.atguigu.school.pojo.Teacher;
import com.atguigu.school.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "教师控制器")
@RequestMapping("/sms/teacherController")
public class TeacherController {

    /**
     * 处理教师相关业务
     */
    @Autowired
    private TeacherService teacherService;

    @PostMapping("/saveOrUpdateTeacher")
    @ApiOperation(value = "新增或更新教师", notes = "没有提供 id 就是新增，否则就是更新")
    public Result saveOrUpdateTeacher(@RequestBody Teacher teacher) {
        this.teacherService.saveOrUpdateTeacher(teacher);
        return Result.ok();
    }

    @DeleteMapping("/deleteTeacher")
    @ApiOperation(value = "批量删除教师")
    @ApiImplicitParam(name = "ids", value = "预删除教师的 id 列表")
    public Result deleteTeacher(@RequestBody List<Integer> ids) {
        this.teacherService.deleteTeachersByIds(ids);
        return Result.ok();
    }

    @GetMapping("/getTeachers/{pageNo}/{pageSize}")
    @ApiOperation(value = "分页查询教师", notes = "目前只支持根据名称和所属班级的名称查")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "pageNo", value = "当前页，从 1 开始"),
            @ApiImplicitParam(name = "pageSize", value = "每页长度")
    })
    public Result getTeachers(@PathVariable("pageNo") Integer pageNo, @PathVariable("pageSize") Integer pageSize, Teacher teacher) {
        Page<Teacher> page = this.teacherService.selectTeachersByNameAndClazzName(teacher.getName(), teacher.getClazzName(), pageNo, pageSize);
        return Result.ok(page);
    }

}
